<template>
  <div class="app-container">
    <el-card shadow="always" class="top-card">
      <el-form :model="queryParams.params" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="queryParams.params.title" placeholder="请输入文章标题" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="文章作者" prop="author">
          <el-input v-model="queryParams.params.author" placeholder="请输入文章作者" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="文章类型" prop="type">
          <el-select v-model="queryParams.params.type" placeholder="文章类型" clearable size="small" style="width: 200px">
            <el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="推荐文章" prop="recommend">
          <el-select v-model="queryParams.params.recommend" placeholder="推荐文章" clearable size="small" style="width: 200px">
            <el-option v-for="dict in recommendOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="置顶" prop="publish">
          <el-select v-model="queryParams.params.top" placeholder="是否置顶" clearable size="small" style="width: 200px">
            <el-option v-for="dict in topOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="always" class="bottom-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5"><el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['manager:article:add']">新增</el-button></el-col>
        <el-col :span="1.5">
          <el-button type="info" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['manager:article:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['manager:article:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="info" icon="el-icon-eye" size="mini" :disabled="single" @click="handleCommentInfo" v-hasPermi="['manager:article:add']">查看评论</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="文章标题" align="center" prop="title">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="toArticleDetail(scope.row.id)">{{ scope.row.title }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="标签" align="center">
          <template slot-scope="scope">
            <el-tag v-for="item in scope.row.yrTags" :key="item.id" :type="item.color" effect="plain" size="mini" style="margin-right: 3px;">{{ item.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="作者" align="center" prop="author" />
        <el-table-column label="阅读量" align="center" prop="readNum" />
        <el-table-column label="评论量" align="center" prop="commentSum" />
        <!-- <el-table-column label="点赞量" align="center" prop="likeNum" /> -->
        <!--  <el-table-column label="封面" align="center" prop="cover"/> -->
        <el-table-column label="发布时间" align="center" prop="createTime" :formatter="row => parseTime(row.createTime, '{y}-{m}-{d}')" />
        <el-table-column label="推荐文章" align="center" prop="recommend" :formatter="row => selectDictLabel(recommendOptions, row.recommend)" />
        <!-- <el-table-column label="分类" align="center" prop="categoryId"/> -->
        <!-- <el-table-column label="发布状态" align="center" prop="publish"/> -->
        <!-- <el-table-column label="是否置顶" align="center" prop="top" :formatter="row => selectDictLabel(topOptions, row.top)" /> -->
        <el-table-column label="文章类型" align="center" prop="type" :formatter="row => selectDictLabel(typeOptions, row.type)" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['manager:article:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['manager:article:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size" @pagination="getList" />

      <!-- 添加或修改文章对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="主键" prop="id"><el-input v-model="form.id" placeholder="请输入主键" /></el-form-item>
          <el-form-item label="文章标题" prop="title"><el-input v-model="form.title" placeholder="请输入文章标题" /></el-form-item>
          <el-form-item label="文章描述" prop="description"><el-input v-model="form.description" placeholder="请输入文章描述" /></el-form-item>
          <el-form-item label="文章作者" prop="author"><el-input v-model="form.author" placeholder="请输入文章作者" /></el-form-item>
          <el-form-item label="html的content" prop="contentFormat"><el-input v-model="form.contentFormat" placeholder="请输入html的content" /></el-form-item>
          <el-form-item label="阅读量" prop="readNum"><el-input v-model="form.readNum" placeholder="请输入阅读量" /></el-form-item>
          <el-form-item label="评论量" prop="commentNum"><el-input v-model="form.commentNum" placeholder="请输入评论量" /></el-form-item>
          <el-form-item label="点赞量" prop="likeNum"><el-input v-model="form.likeNum" placeholder="请输入点赞量" /></el-form-item>
          <el-form-item label="封面" prop="cover"><el-input v-model="form.cover" placeholder="请输入封面" /></el-form-item>
          <el-form-item label="创建时间" prop="createTime"><el-input v-model="form.createTime" placeholder="请输入创建时间" /></el-form-item>
          <el-form-item label="更新时间" prop="updateTime"><el-input v-model="form.updateTime" placeholder="请输入更新时间" /></el-form-item>
          <el-form-item label="是否推荐文章" prop="recommend"><el-input v-model="form.recommend" placeholder="请输入是否推荐文章" /></el-form-item>
          <el-form-item label="分类类别存在多级分类，用逗号隔开" prop="categoryId">
            <el-input v-model="form.categoryId" placeholder="请输入分类类别存在多级分类，用逗号隔开" />
          </el-form-item>
          <el-form-item label="发布状态" prop="publish"><el-input v-model="form.publish" placeholder="请输入发布状态" /></el-form-item>
          <el-form-item label="是否置顶" prop="top"><el-input v-model="form.top" placeholder="请输入是否置顶" /></el-form-item>
          <el-form-item label="文章类型" prop="type"><el-input v-model="form.type" placeholder="请输入文章类型" /></el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { listArticle, infoArticle, removeArticle, addArticle, editArticle } from '@/api/manager/article';
export default {
  name: 'Article',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      articleList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        params: {
          //文章标题
          title: undefined,
          //文章作者
          author: undefined,
          //封面
          cover: undefined,
          //是否推荐文章
          recommend: undefined,
          //分类类别存在多级分类，用逗号隔开
          categoryId: undefined,
          //发布状态
          publish: undefined,
          //是否置顶
          top: undefined,
          //文章类型
          type: undefined
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: []
      },
      typeOptions: [],
      topOptions: [],
      recommendOptions: []
    };
  },
  created() {
    this.getDicts('manager_article_type').then(res => {
      this.typeOptions = res.data;
    });
    this.getDicts('manager_yes_no').then(res => {
      this.topOptions = res.data;
      this.recommendOptions = res.data;
    });
    this.getList();
  },
  methods: {
    /** 查询文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(res => {
        console.log(res.data.records);
        this.articleList = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        description: undefined,
        author: undefined,
        contentFormat: undefined,
        readNum: undefined,
        commentNum: undefined,
        likeNum: undefined,
        cover: undefined,
        createTime: undefined,
        updateTime: undefined,
        recommend: undefined,
        categoryId: undefined,
        publish: undefined,
        top: undefined,
        type: undefined
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => {
        return item.id;
      });
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/manager/article/add' });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.reset();
      const id = row.id || this.ids[0];
      this.$router.push({ path: '/manager/article/update', query: { articleId: id } });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeArticle(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    toArticleDetail(id) {
      window.open('http://jy520.fun/article/articleContent?id=' + id, '_blank');
    },
    handleCommentInfo(row){
      this.$router.push({ path: `manager/comment/index/${this.ids[0]}` })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editArticle(this.form).then(res => {
              if (res.code === 200) {
                this.msgSuccess('修改成功');
                this.open = false;
                this.getList();
              } else {
                this.msgError(res.msg);
              }
            });
          } else {
            addArticle(this.form).then(res => {
              if (res.code === 200) {
                this.msgSuccess('新增成功');
                this.open = false;
                this.getList();
              } else {
                this.msgError(res.msg);
              }
            });
          }
        }
      });
    }
  }
};
</script>

<style>
.top-card .el-card__body {
  padding-bottom: 0;
}
.bottom-card {
  margin-top: 10px;
}
</style>
